package com.yunze.system.card;

import com.alibaba.fastjson.JSON;
import com.yunze.common.mapper.mysql.YzExecutionTaskMapper;
import com.yunze.common.mapper.mysql.card.ApplyUseCardMapper;
import com.yunze.common.utils.yunze.WriteCSV;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 申请用卡导出
 */
@Slf4j
@Component
public class ApplyUseCardOut {


    @Resource
    private ApplyUseCardMapper applyUseCardMapper;
    @Resource
    private YzExecutionTaskMapper yzExecutionTaskMapper;
    @Resource
    private WriteCSV writeCSV;


    @RabbitHandler
    @RabbitListener(queues = "admin_applyUseCardOut_queue")
    public void applyUseCardOut(String msg){

        try {
            if (StringUtils.isEmpty(msg)) {
                return;
            }
            Map<String,Object> Pmap = JSON.parseObject(msg);
            Map<String, Object> map = (Map<String, Object>) Pmap.get("map");
            Map<String, Object> task_map = (Map<String, Object>) Pmap.get("task_map");

            List<Map<String, Object>> applyState = (List<Map<String, Object>>) Pmap.get("applyState");
            List<Map<String, Object>> DeptArr = (List<Map<String, Object>>) Pmap.get("DeptArr");
            String newName =  Pmap.get("newName").toString();

            //执行
            OutData( map,applyState,DeptArr,task_map,newName);
        }catch (Exception e){
            log.error(">>错误 - 客户资费导出 消费者:{}<<", e.getMessage());
        }
    }


    public void OutData(Map<String, Object> map,List<Map<String, Object>> applyState,List<Map<String, Object>> DeptArr,Map<String, Object> task_map,String newName){

        yzExecutionTaskMapper.add(task_map);//添加执行 任务表


        List<Map <String,Object>> mapList =  applyUseCardMapper.selMap(map);

        List<Map<String, Object>> out_list = new ArrayList<>();

        if(mapList!=null && mapList.size()>0){
            String Outcolumns[] = {"ID","使用人姓名","身份证号","身份证地址","使用人电话","ICCID","创建时间","审核时间","状态","审核建议","流水号","订购号码","所属企业","申请备注"};
            String keys[] = {"id","u_name","id_number","id_address","telephone","iccid","create_time","review_time","status_value","audit_suggestion","a_no","apply_telephone","dept_value","apply_remarks"};
            for (int i = 0; i < mapList.size(); i++) {
                Map<String, Object> out_map = mapList.get(i);
                out_map  =  WriteDic(out_map,applyState,"state","status_value");

                out_map  =  WriteDept(out_map,DeptArr,"dept_id","dept_value");
                out_list.add(out_map);
                if ((i+1)%50==0 || (i+1)==mapList.size()){
                    //执行导出
                    if(out_list.size()>0){
                        writeCSV.Write(newName,out_list,Outcolumns,null,keys);
                        out_list = new ArrayList<>();
                    }
                }
            }
        }
        yzExecutionTaskMapper.set_end_time(task_map);//任务结束
    }

    /**
     * 字典获取
     * @param CarMap 写入Map
     * @param DictDataArr 字典 list
     * @param basis 获取依据 字段 与 dictValue 判断
     * @param fieldName 返回字段名称
     * @return
     */
    public Map<String,Object> WriteDic (Map<String,Object> CarMap,List<Map<String, Object>> DictDataArr,String basis,String fieldName){

        Map<String,Object> Rmap = new HashMap<String,Object>();
        String status_id = CarMap.get(basis)!=null?CarMap.get(basis).toString():null;
        boolean bool = false;
        if(status_id!=null){
            for (int i = 0; i < DictDataArr.size(); i++) {
                Map<String, Object>  dictData =  DictDataArr.get(i);
                String dictValue = dictData.get("dictValue").toString();
                if(dictValue.equals(status_id)){
                    CarMap.put(fieldName,dictData.get("dictLabel"));
                    bool = true;
                    break;
                }
            }
        }
        //字段 默认值
        if(!bool){
            CarMap.put(fieldName,"");
        }
        return CarMap;
    }

    public Map<String,Object> WriteDept (Map<String,Object> CarMap,List<Map<String, Object>> DictDataArr,String basis,String fieldName){
        String status_id = CarMap.get(basis)!=null?CarMap.get(basis).toString():null;
        boolean bool = false;
        if(status_id!=null){
            for (int i = 0; i < DictDataArr.size(); i++) {
                Map<String, Object>  dictData =  DictDataArr.get(i);
                String dept_id = dictData.get("dept_id").toString();
                if(dept_id.equals(status_id)){
                    CarMap.put(fieldName,dictData.get("dept_name"));
                    bool = true;
                    break;
                }
            }
        }
        //字段 默认值
        if(!bool){
            CarMap.put(fieldName,"");
        }
        return CarMap;
    }




}
